package cn.gjculture.shopping.api.controller;

import cn.gjculture.shopping.api.entity.PageInfo;
import cn.gjculture.shopping.api.entity.PostageTemplate;
import cn.gjculture.shopping.api.entity.Return;
import cn.gjculture.shopping.api.service.PostageTemplateService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/postageTemplate")
public class PostageTemplateController {
    @Autowired
    PostageTemplateService postageTemplateService;

    @ApiOperation(value = "新增邮费模板", notes = "新增邮费模板")
    @ApiImplicitParam(name = "postageTemplate", value = "邮费模板对象", required = true, dataType = "Object")
    @PostMapping
    public Return save(PostageTemplate postageTemplate) {
        Return ret = new Return();
        if(postageTemplateService.insert(postageTemplate)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @ApiOperation(value = "修改邮费模板", notes = "修改邮费模板")
    @ApiImplicitParam(name = "postageTemplate", value = "邮费模板对象", required = true, dataType = "Object")
    @PutMapping
    public Return update(PostageTemplate postageTemplate){
        Return ret=new Return<>();
        if(postageTemplateService.update(postageTemplate)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @ApiOperation(value = "获取邮费模板列表", notes = "获取邮费模板列表")
    @GetMapping
    public Return<List<PostageTemplate>> query(PostageTemplate template, PageInfo pageInfo) {
        Return<List<PostageTemplate>> ret = new Return<>();
        List<PostageTemplate> postageTemplates = postageTemplateService.query(template);
        if(!CollectionUtils.isEmpty(postageTemplates)){
            ret.getCommon().setRet(true);
        }
        ret.setData(postageTemplates);
        return ret;
    }



    @ApiOperation(value = "根据ID获取邮费模板", notes = "根据ID获取邮费模板")
    @ApiImplicitParam(name = "postageTemplateId", value = "邮费模板ID", required = true, dataType = "String",paramType = "path")
    @GetMapping(value = "/{postageTemplateId}")
    public Return<PostageTemplate> getAddressById(@PathVariable(value = "postageTemplateId") String postageTemplateId) {
        Return<PostageTemplate> ret = new Return<>();
        PostageTemplate postageTemplate = postageTemplateService.queryById(postageTemplateId);
        if(postageTemplate!=null){
            ret.getCommon().setRet(true);
        }
        ret.setData(postageTemplate);
        return ret;
    }


    @ApiOperation(value = "删除邮费模板", notes = "删除邮费模板")
    @ApiImplicitParam(name = "postageTemplateId", value = "邮费模板ID", required = true, dataType = "String",paramType = "path")
    @DeleteMapping(value = "/{postageTemplateId}")
    public Return delete(@PathVariable(value = "postageTemplateId") String postageTemplateId,boolean withUsed) {
        Return ret = new Return<>();
        if(postageTemplateService.delete(postageTemplateId,withUsed,ret)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }
}
